public class PayMoneyActivity  extends Activity {
	
    private MoneyNumInputView mniView;
    private GridView gridView;
    private ArrayList<Map<String, String>> valueList;
    private EditText textAmount;
    private RelativeLayout back;
    private TextView btn_pay;
    private static String money;;
    private DialogPayMoney dialog; 
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_normal_key_board);
        StatusBarCompat.setStatusBarColor(this, 0xffededed);
        dialog=new DialogPayMoney(this,"微信支付");
        initView();
        valueList = virtualKeyboardView.getValueList();
    }

    private void initView() {
        textAmount = (EditText) findViewById(R.id.textAmount);
        back=(RelativeLayout)findViewById(R.id.wx_6_num_back);
        btn_Pay=(TextView)findViewById(R.id.wx_pay_zz_btn);
        btn_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!textAmount.getText().toString().equals("")){
                    money=guifan2(textAmount.getText().toString());
                    if(money.equals("0.00")){
                        Toast.makeText(NormalKeyBoardActivity.this,"金额不能为0元",Toast.LENGTH_SHORT).show();
                    }else {
                        startInputPwd();
                    }
                }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("money", MoneyUtil.FAIL);
                setResult(MoneyUtil.Intent_finish, intent);
                finish();
            }
        });

        //不调用系统键盘
        if (android.os.Build.VERSION.SDK_INT <= 10) {
            textAmount.setInputType(InputType.TYPE_NULL);
        } else {
            this.getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            try {
                Class<EditText> cls = EditText.class;
                Method setShowSoftInputOnFocus;
                setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus",  boolean.class);
                setShowSoftInputOnFocus.setAccessible(true);
                setShowSoftInputOnFocus.invoke(textAmount, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        mniView = (MoneyNumInputView) findViewById(R.id.mniView);
        mniView.getLayoutBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mniView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.push_bottom_out));
                mniView.setVisibility(View.GONE);
            }
        });
        gridView = mniView.getGridView();
        gridView.setOnItemClickListener(onItemClickListener);
		//对edittext设置虚拟按键的点击监听
        textAmount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mniView.setFocusable(true);
                mniView.setFocusableInTouchMode(true);
                mniView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.push_bottom_in));
                mniView.setVisibility(View.VISIBLE);
            }
        });

    }

	//对girdView设置点击监听，并且对小数点进行规范
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (position < 11 && position != 9) {//点击0~9
                //按下0-9 检查是否有小数点  有的话小数点后两位就不写
                if(textAmount.getText().toString().length()>2){
                    String a=textAmount.getText().toString();
                    if(a.substring(a.length()-3,a.length()-2).equals(".")){
                    }else {
                        String amount = textAmount.getText().toString().trim();
                        amount = amount + valueList.get(position).get("name");
                        textAmount.setText(amount);
                        Editable ea = textAmount.getText();
                        textAmount.setSelection(ea.length());
                    }
                }else {
                    String amount = textAmount.getText().toString().trim();
                    amount = amount + valueList.get(position).get("name");
                    textAmount.setText(amount);
                    Editable ea = textAmount.getText();
                    textAmount.setSelection(ea.length());
                }
            } else  if (position == 9) {//点击.键
                //TODO 按下小数点 是第一位就加上0
                String amount = textAmount.getText().toString().trim();
                if(textAmount.getText().toString().length()==0){
                    amount = amount + valueList.get(position).get("name");
                    textAmount.setText("0"+amount);
                    Editable ea = textAmount.getText();
                    textAmount.setSelection(ea.length());
                }else {
                    if (!amount.contains(".")) {
                        amount = amount + valueList.get(position).get("name");
                        textAmount.setText(amount);
                        Editable ea = textAmount.getText();
                        textAmount.setSelection(ea.length());
                    }
                }
            }else if (position == 11) {//点击退格键
                    String amount = textAmount.getText().toString().trim();
                    if (amount.length() > 0) {
                        amount = amount.substring(0, amount.length() - 1);
                        textAmount.setText(amount);
                        Editable ea = textAmount.getText();
                        textAmount.setSelection(ea.length());
                    }
                }
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==MoneyUtil.SUCCESS){
            Intent intent = new Intent();
            intent.putExtra("money", money);
            setResult(MoneyUtil.intent_finish, intent);
            finish();
        }else {
            finish();
        }
    }
    @Override
    public void onBackPressed(){
        Intent intent = new Intent();
        intent.putExtra("money", MoneyUtil.FAIL);
        setResult(MoneyUtil.intent_finish, intent);
        finish();
    }

	//把输入金额规范为小数点后两位
    private String guifan2(String s){
        String sb="";
        if(!s.contains(".")){
            sb=s+".00";
        }else {
            if(s.substring(s.length()-2,s.length()-1).equals(".")){
                sb=s+"0";
            }else if(s.substring(s.length()-1,s.length()).equals(".")){
                sb=s+"00";
            }else {
                sb=s;
            }
        }
        double a= Double.valueOf(sb);
        DecimalFormat df=new DecimalFormat("0.00");
        sb=df.format(a)+"";
        return sb;
    }
	
	//金额输入完毕，开始输入密码
	private startInputPwd(){
		dialog.show();
		new Handler().postDelayed(new Runnable(){
				public void run() {
					try{
						dialog.dismiss();
						PopEnterPassword popEnterPassword = new PopEnterPassword(NormalKeyBoardActivity.this,money,0);
						popEnterPassword.showAtLocation(NormalKeyBoardActivity.this.findViewById(R.id.root_pay), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
					}catch (Exception e){
					}
				}
			}, 1200);
	}


}

